/*
 * @Author: szx
 * @Date: 2022-03-02 13:00:30
 * @LastEditTime: 2022-03-02 13:47:56
 * @Description:
 * @FilePath: \leetcode\1-99\43\43.js
 */
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
 var multiply = function (num1, num2) {
    if (num1 === '0' || num2 === '0') return '0';
    const m = num1.length;
    const n = num2.length;
    if (n < m) multiply(num2, num1);
    const arr = new Array(m + n).fill(0);
    let mul = 0;
    let x = 0,
        y = 0;
    for (let i = m - 1; i >= 0; i--) {
        mul = 0;
        y = num1[i] - '0';
        for (let j = n - 1; j >= 0; j--) {
            x = num2[j] - '0';
            const tmp = x * y + mul + arr[i + j + 1];
            arr[i + j + 1] = tmp % 10;
            mul = Math.floor(tmp / 10);
        }
        arr[i] = mul;
    }
    arr[0] = mul;
    if (arr[0] == 0) arr.shift();
    return arr.join('');
};
console.log(multiply('99', '99'));
